package com.schooleducation.controller.teacher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.ExamDicType;
import com.schooleducation.entity.ExamDictinoary;
import com.schooleducation.entity.Exams;
import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonOneType;
import com.schooleducation.entity.LessonTwoType;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

@Controller
@RequestMapping("/teacher/publishExam")
public class PublishExamController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	List<LessonTwoType> LessonTwoTypes;
	List<ExamDicType> DicTypes;

	/**
	 * 获取发布考试页面
	 * 
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(Model model, HttpSession session, ExamDictinoary examDictinoary) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			List<LessonOneType> lessonOneTypes = serviceFac.getLessonService().getLessonTypeAll();
			List<LessonTwoType> lessonTwoTypes = new ArrayList<LessonTwoType>();
			for (LessonOneType lessonOneType : lessonOneTypes) {
				for (LessonTwoType lessonTwoType : lessonOneType.getLessonTwoType()) {
					lessonTwoTypes.add(lessonTwoType);
				}
			}
			List<ExamDicType> dicTypes = serviceFac.getExamsService().getExamDicTypes();
			model.addAttribute("lessonTwoTypes", lessonTwoTypes);
			LessonTwoTypes = lessonTwoTypes;
			model.addAttribute("dicTypes", dicTypes);
			DicTypes = dicTypes;
			List<Lesson> lessons=serviceFac.getLessonService().getLessonListByTeacher(teacher);
			model.addAttribute("lessons",lessons);
			return "teacher/publishExam";
		} else {
			return "redirect:../login";
		}
	}

//	@RequestMapping(method = RequestMethod.POST)
//	public String getExamDic(@RequestParam("dicTypeId") Integer dicTypeId, @RequestParam("lesTypeId") Integer lesTypeId,
//			Model model) {
//		model.addAttribute("lessonTwoTypes", LessonTwoTypes);
//		model.addAttribute("dicTypes", DicTypes);
//		if (dicTypeId != null && lesTypeId != null) {
//			ExamDicType examDicType = new ExamDicType();
//			examDicType.setId(dicTypeId);
//			LessonTwoType lessonTwoType = new LessonTwoType();
//			lessonTwoType.setId(lesTypeId);
//			ExamDictinoary examDictinoary = new ExamDictinoary();
//			examDictinoary.setExamType(examDicType);
//			examDictinoary.setLessonType(lessonTwoType);
//			List<ExamDictinoary> examDictinoaries = serviceFac.getExamsService().getExamDictinoary(examDictinoary, 0);
//			model.addAttribute("examDictinoaries", examDictinoaries);
//		}
//
//		return "teacher/publishExam";
//	}

	//获取实体列表
	@RequestMapping(method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getExamDic(String data) {
		ExamDictinoary examDictinoary = (ExamDictinoary) JSONObject.toBean(JSONObject.fromObject(data),
				ExamDictinoary.class);
		List<ExamDictinoary> examDictinoaries = serviceFac.getExamsService().getExamDictinoary(examDictinoary, 0);
		Map<String, Object> map = new HashMap<>();
		map.put("message", "success");
		map.put("exams", examDictinoaries);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//去死循环
		jsonConfig.setExcludes(new String[]{"examType","lessonType"});//去延时加载字段
		JSONObject json =JSONObject.fromObject(map, jsonConfig);
		return json.toString();
	}
	
	//发布试卷 
	@RequestMapping(value="put",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String putExam(String title,String data,String keys,String time,String lesson) {
		Exams exams=new Exams();
		exams.setTitle(title);
		exams.setData(data);
		exams.setTime(Integer.valueOf(time));
		exams.setAnswer(keys);
		Lesson les=new Lesson();
		les.setId(Integer.valueOf(lesson));
		exams.setLesson(les);
		exams=serviceFac.getExamsService().addExam(exams);
		Map<String, Object> map = new HashMap<>();
		map.put("message", "success");
		map.put("examID", exams.getId());
		return JSONObject.fromObject(map).toString();
	}
}
